package org.yinxue.swing.core.panel;

import fun.codedesign.yinxue.config.UserConfig;
import fun.codedesign.yinxue.util.event.MailBox;
import fun.codedesign.yinxue.util.thread.ThreadPool;
import org.yinxue.swing.core.standard.YxPanel;
import org.yinxue.swing.core.standard.YxStructure;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class YxUnitPanel extends YxPanel implements YxStructure {
    protected JPanel topButton;
    protected JLabel jLabel_project;
    protected JTextField jText_inputpath;

    protected JButton jButton_choose;
    protected  JButton jButton_open;
    protected JButton jButton_diy;
    protected JButton jButton_generate;

    protected JLabel jLabel_unit;
    protected JComboBox<String> jComboBox_unitType;

    protected JPanel jPanel_operate;
    protected JLabel jLabel_operate;
    protected JComboBox jComboBox_operateType;

    protected JCheckBox jCheckBox_isPojo;

    protected YxTextArea topInput;
    protected YxTextArea downOutput;

    protected YxTextArea rightInfo;

    // 数据类操作
    protected ThreadPool threadPool;
    protected MailBox mailBox;

    public YxUnitPanel() {
        super();
    }

    @Override
    public void init() {
        topButton = createYxPanel();
        jLabel_project = createLabel("项目路径:");
        jText_inputpath = createTextField(10);
        jButton_choose = createButton("选择路径", 110, 30);

        jButton_open = createButton("开启容器", 110, 30);
        jButton_diy = createButton("自定义参数", 110, 30);
        jButton_generate = createButton("一键执行", 110, 30);

        jLabel_unit = createLabel("单元测试类型:");

        Font timeNewRoman = new Font("Timenew Romans", Font.PLAIN, 12);
        jComboBox_unitType = createComboBox("Mockito", "JUnit", "SpringTest");
        jComboBox_unitType.setPreferredSize(new Dimension(130, 25));
        jComboBox_unitType.setFont(timeNewRoman);


        jPanel_operate = createFlowPanel();
        ;
        jLabel_operate = createLabel("生成方式:");

        jComboBox_operateType = createComboBox("单个生成", "批量生成", "批量回滚");
        jComboBox_operateType.setPreferredSize(new Dimension(130, 25));
        jComboBox_operateType.setFont(timeNewRoman);

        jCheckBox_isPojo = createCheckBox("强制实体类");

        topInput = creaetYxTextArea();
        downOutput = creaetYxTextArea();
        rightInfo = creaetYxTextArea();

        jText_inputpath.setFont(timeNewRoman);

    }


    @Override
    public void position() {
        // 面板排布
        add(topButton, refreshPosition(0, 0, 2, 1, 1, 0, GridBagConstraints.HORIZONTAL));
        add(topInput, refreshPosition(0, 1, 1, 1, 1, 1, GridBagConstraints.BOTH));
        add(downOutput, refreshPosition(0, 2, 1, 1, 1, 1, GridBagConstraints.BOTH));
        add(rightInfo, refreshPosition(1, 1, 1, 2, 0, 0, GridBagConstraints.BOTH));


        jPanel_operate.add(jLabel_operate);
        jPanel_operate.add(jComboBox_operateType);
        jPanel_operate.add(jCheckBox_isPojo);

        Dimension size = new Dimension(200, 400);
        rightInfo.setPreferredSize(size);
        rightInfo.setMaximumSize(size);
        rightInfo.setMinimumSize(size);


        GridBagConstraints g = new GridBagConstraints();
        g.insets = new Insets(5, 5, 5, 5);
        topButton.setBorder(BorderFactory.createEtchedBorder());
        g.gridx = 0;
        g.gridy = 0;
        topButton.add(jLabel_project, g);

        g.gridx = 1;
        g.weightx = 1;
        g.fill = GridBagConstraints.HORIZONTAL;
        g.gridwidth = 2;
        topButton.add(jText_inputpath, g);

        g.weightx = 0;
        g.gridwidth = 1;
        g.gridx = 3;
        topButton.add(jButton_choose, g);

        g.gridx = 4;
        topButton.add(jButton_open, g);

        g.gridx = 0;
        g.gridy = 1;
        topButton.add(jLabel_unit, g);

        g.gridx = 1;
        topButton.add(jComboBox_unitType, g);


        g.gridx = 2;
        g.weightx = 1;
        g.fill = GridBagConstraints.HORIZONTAL;
        topButton.add(jPanel_operate, g);

        g.weightx = 0;
        g.gridx = 3;
        topButton.add(jButton_diy, g);

        g.gridx = 4;
        topButton.add(jButton_generate, g);

    }

    @Override
    public void action() {
        // 由于是在构造器中初始化，需要new出新对象
        jText_inputpath.setText(UserConfig.getUserProjectPath());
        threadPool = ThreadPool.singleton();
        mailBox = new MailBox();

        // 开启一个线程监听信息，展示在textArea区域
        threadPool.submitListener(() -> {
            while (!Thread.interrupted()) {
                rightInfo.append(mailBox.acceptMessage());
            }
        });
        jButton_choose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser("");
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.setMultiSelectionEnabled(true);
                int open = chooser.showOpenDialog(null);
                if (JFileChooser.APPROVE_OPTION == open) {
                    String filePath = chooser.getSelectedFile().getAbsolutePath();
                    jText_inputpath.setText(filePath);
                    getAndSetInputPath();
                } else {
                    // doNoting
                }
            }
        });
        // 子类实现逻辑
    }

    public String getAndSetInputPath() {
        String projectPath = jText_inputpath.getText();
        UserConfig.saveUserProjectPath(projectPath);
        return projectPath;
    }

    public MailBox mailBox() {
        return mailBox;
    }

    public ThreadPool threadPool() {
        return threadPool;
    }

    public static void main(String[] args) {
        run(YxUnitPanel.class);
    }
}
